package com.globallogic.dailyplanner.jsfbeans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
import org.springframework.stereotype.Controller;

import com.globallogic.dailyplanner.dto.RoleEnum;
import com.globallogic.dailyplanner.dto.User;
import com.globallogic.dailyplanner.service.UserService;

@Controller
@ManagedBean
@RequestScoped
public class RegistrationBean {

	@Autowired
	private UserService userService;

	@Autowired
	private ShaPasswordEncoder encoder;

	private User user;

	public void initUser() {
			user = new User();
		
	}

	public String save() {
		User user2 = new User();
		user2.setUsername(user.getUsername());
		user2.setPassword(encoder.encodePassword(user.getPassword(), ""));
		user2.setFullName(user.getFullName());
		user2.setNotes(user.getNotes());
		user2.setActive(false);
		user2.setRole(RoleEnum.USER);
		user2.setEmail(user.getEmail());

		userService.save(user2);
		return "register_success";
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
